package com.ruoyi.supermarket.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.supermarket.bean.OrderRecordRes;
import com.ruoyi.supermarket.domain.OrderItem;
import com.ruoyi.supermarket.service.OrderItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.supermarket.domain.OrderRecord;
import com.ruoyi.supermarket.service.OrderRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单记录Controller
 * 
 * @author 李其伟
 * @date 2023-11-13
 */
@RestController
@RequestMapping("/supermarket/orderRecord")
public class OrderRecordController extends BaseController
{
    @Autowired
    private OrderRecordService orderRecordService;

    @Autowired
    private OrderItemService orderItemService;
    /**
     * 查询订单记录列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:orderRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderRecord orderRecord)
    {
        startPage();
        List<OrderRecord> list = orderRecordService.selectOrderRecordList(orderRecord);
        TableDataInfo tableDataInfo =  getDataTable(list);

        List<OrderRecord> orderRecordList = (List<OrderRecord>) tableDataInfo.getRows();
        if(orderRecordList.size()==0){
            return tableDataInfo;
        }

        String[] orderSns = orderRecordList.stream().map(OrderRecord::getOrderSn).toArray(String[]::new);
        List<OrderItem> orderItemList = orderItemService.selectOrderItemByOrderSn(orderSns);
        Map<String,List<OrderItem>> orderItemMap = orderItemList.stream().collect(Collectors.groupingBy(OrderItem::getOrderSn));

        List<OrderRecordRes> orderRecordResList = new ArrayList<>();
        for(OrderRecord or : orderRecordList){
            OrderRecordRes orr = new OrderRecordRes();
            orr.setOrderRecord(or);
            orr.setOrderItemList(orderItemMap.get(or.getOrderSn()));
            orderRecordResList.add(orr);
        }
        tableDataInfo.setRows(orderRecordResList);
        return tableDataInfo;
    }

    /**
     * 导出订单记录列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:orderRecord:export')")
    @Log(title = "订单记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderRecord orderRecord)
    {
        List<OrderRecord> list = orderRecordService.selectOrderRecordList(orderRecord);
        ExcelUtil<OrderRecord> util = new ExcelUtil<OrderRecord>(OrderRecord.class);
        util.exportExcel(response, list, "订单记录数据");
    }

    /**
     * 获取订单记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('supermarket:orderRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(orderRecordService.selectOrderRecordById(id));
    }

    /**
     * 新增订单记录
     */
    @PreAuthorize("@ss.hasPermi('supermarket:orderRecord:add')")
    @Log(title = "订单记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderRecord orderRecord)
    {
        return toAjax(orderRecordService.insertOrderRecord(orderRecord));
    }

    /**
     * 修改订单记录
     */
    @PreAuthorize("@ss.hasPermi('supermarket:orderRecord:edit')")
    @Log(title = "订单记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderRecord orderRecord)
    {
        return toAjax(orderRecordService.updateOrderRecord(orderRecord));
    }

    /**
     * 删除订单记录
     */
    @PreAuthorize("@ss.hasPermi('supermarket:orderRecord:remove')")
    @Log(title = "订单记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(orderRecordService.deleteOrderRecordByIds(ids));
    }
}
